<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getSystemDictionary">查询</el-button>
        </el-form-item>
        <el-form-item>
          <!--v-perm="'menu:add'"-->
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="SystemDictionary" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="id" label="ID" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="typesId" label="类型ID" width="120" sortable>
      </el-table-column>
      <!-- 操作列 -->
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length === 0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <!-- 新增相关字段 -->
        <el-form-item label="ID" prop="id">
          <el-input v-model="editForm.id" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型ID">
          <el-select v-model="editForm.typesId" placeholder="请选择类型">
            <el-option
                v-for="type in types"
                :key="type.id"
                :label="type.name"
                :value="type.id">
              <span style="float: left">{{ type.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ type.id }}
            </span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      /*1.分页数据查询参数*/
      total: 0,
      currentPage: 1,
      pageSize: 5,
      /*2.部门分页数据*/
      SystemDictionary: [],
      /*6. 编辑框标题绑定*/
      title: "",
      /*高级查询keyword*/
      keyword: "",
      listLoading: false,
      sels: [],//列表选中列
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {

      },
      //编辑界面数据
      editForm: {
          id: null,
          name: "",
          typesId: null
      },
      types:[]
    }
  },
  methods: {
    /*1.获取部门列表*/
    getSystemDictionary() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;
      //then删除数据  请求响应没有问题，级便数据没有删除成功，走then       catch请求异常404  400 500
      this.$http.post("/systemDictionary", para).then(result => {
        let { totals, data } = result.data;
        this.total = totals;
        this.SystemDictionary = data
        this.listLoading = false;
      }).catch(result => {

      })
    },
    /*2.页数改变获取分页数据函数*/
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSystemDictionary();
    },
    /*3.根据ID删除*/
    handleDel: function (index, row) {
      this.$confirm('确定删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //result   AjaxResult
        this.$http.delete("/systemDictionary/" + row.id).then(result => {
          let { success, message } = result.data;
          if (success) {  //请求业务是否成功
            this.$message({
              message: message,
              type: 'success'
            });
            if ((this.total - 1) % this.pageSize == 0) { //说明已经是在删除当前页得最后一条： 当前页减一之后在查询
              this.currentPage -= 1;
            }
            this.getSystemDictionary();
          } else {
            this.$message({
              message: message,
              type: 'error'
            });
          }
          this.listLoading = false;
        }).catch(result => {

        })
      }).catch(() => {

      });
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },
    /*4.批量删除*/
    batchRemove: function () {
      //[{id:1,name:xx},id:2,name:xx},id:3,name:xx}]   [1,2,3]   "[1,2,3]"
      var ids = this.sels.map(item => item.id)  //[1,2,3]
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/systemDictionary", ids).then(result => {
          let { success, message } = result.data
          if (success) {
            this.$message({
              message: message,
              type: 'success'
            });
            if ((this.total - ids.length) % this.pageSize == 0) {
              this.currentPage -= 1;
            }
            this.getSystemDictionary()
          } else {
            this.$message({
              message: message,
              type: 'error'
            });
          }
          this.listLoading = false;
        }).catch(result => {

        })
      }).catch(() => {

      });
    },
    /*5.表格选项改变触发事件*/
    selsChange: function (sels) {
      this.sels = sels;
    },
    /*6 新增:  显示编辑界面*/
    handleAdd: function () {
      this.getType();
      this.editFormVisible = true;
      this.title = "新增"
      this.editForm = {
        id: null,
        name: "",
        typesId: null
        }
      },

    /*7.修改：显示编辑界面*/
    handleEdit: function (index, row) {
      this.getType();
      this.title = "编辑"
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);  //拷贝row给到editForm
      // 处理员工信息
      if (!this.editForm.systemDictionary) {
        this.editForm.systemDictionary = {
          id: null,
          name: "",
          typesId: null
        };
      }
      console.log("row", row)
    }, /*8.添加和修改，提交数据*/
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let systemDictionary = Object.assign({}, this.editForm);
            this.$http.put("/systemDictionary",systemDictionary).then(result=>{
              this.editLoading = false;
              this.editFormVisible = false;
              let {success,message} = result.data;
              if(success){
                this.$message({type:"success",message:message})
                this.getSystemDictionary();
              }else{
                this.$message({type:"error",message:message})
              }
            }).catch(result=>{

            })
          });
        }
      });
    },
    /*9.类型ID*/
    getType(){
      this.$http.get("/systemDictionarytypetype").then(result=>{
        this.types = result.data
      }).catch(result=>{})
    },

  },
  mounted(){
    this.getSystemDictionary();
  }
}
</script>

<style scoped>

</style>